package music.Share;

import java.util.ArrayList;



import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;



public class Eingang extends Activity {
	private String uid;
	private ArrayList<String> links;
	private ListView lv;
	private ArrayList<Datensatz> datensaetze;
	private MyItemAdapter meinadapter;

	
	
	
	public void initialize() {
		uid = ((TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
		lv = (ListView)findViewById(R.id.einganglv);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void filllistview() {

		new AsyncTask() {
    		@Override
			protected Object doInBackground(Object... params) 
    			{
    				links=Methoden.EingehendeOrdner(uid);	
    				datensaetze = new ArrayList<Datensatz>();
    			    for (String singlelink:links) 
    			    	{
    			    	Datensatz datensatz = new Datensatz(singlelink,singlelink.substring(singlelink.lastIndexOf('/') + 1));
    			    	datensaetze.add(datensatz);
    			    	}

				return null;
				}
				

			@Override
			protected void onPostExecute(Object result) 
				{
				super.onPostExecute(result);
			    if (links.isEmpty())
			    	{
			    	((TextView) findViewById(R.id.leermeldung)).setVisibility(View.VISIBLE);
			    	setResult(Activity.DEFAULT_KEYS_SHORTCUT);
			    	}
				    	
			    meinadapter= new MyItemAdapter(Eingang.this, datensaetze, "Eingang", uid);
			    	
			    lv.setAdapter(meinadapter);
//								lv.setOnItemClickListener(meinadapter);	
				((ProgressBar) findViewById(R.id.progressBar1)).setVisibility(View.INVISIBLE);
				}

			@Override
			protected void onPreExecute() 
				{
				super.onPreExecute();
				((ProgressBar) findViewById(R.id.progressBar1)).setVisibility(View.VISIBLE);
				}
			
		}.execute();
	}
	
    
	@Override
	public void onCreate(Bundle savedInstanceState) 
		{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.eingang);
		
		initialize();
		filllistview();
	    }


	
		@Override 
		public void onActivityResult(int requestCode, int resultCode, Intent data) 
			{     
			super.onActivityResult(requestCode, resultCode, data); 
		     if (requestCode == 1 && resultCode == Activity.RESULT_OK) 
		    	{ 
	    		lv.setAdapter(null);
	    		filllistview();
		      	} 
			}
		
		
	    @Override
	    public void onConfigurationChanged(Configuration newConfig)
	    	{
	        super.onConfigurationChanged(newConfig);
	        System.out.println("Bildschirm gedreht!");
	    	}
	    
	}

